package cn.yunhe.test;

import cn.yunhe.dao.BoardMapper2;
import cn.yunhe.dao.NewsMapper2;
import cn.yunhe.entity.Board;
import cn.yunhe.entity.News;
import cn.yunhe.util.SqlSessionFactoryUtil2;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test2 {

    SqlSession sqlSession=null;
    NewsMapper2 newsMapper2=null;
    BoardMapper2 boardMapper2=null;

    @Before
    public void init(){
        sqlSession=new SqlSessionFactoryUtil2().getSqlSession();
        newsMapper2=sqlSession.getMapper(NewsMapper2.class);
        boardMapper2=sqlSession.getMapper(BoardMapper2.class);
    }

    @Test
    public void test1(){
        List<News> newsList=newsMapper2.getAllNews();
        for(News n:newsList){
            System.out.println(n.getNewsId()+","+n.getTitle()+","+n.getContext()+","+n.getWriteDate()+"......");
        }
    }

    @Test
    public void test2(){
        News n=sqlSession.selectOne("getNewsById",3);
        System.out.println(n.getNewsId()+","+n.getTitle()+","+n.getContext()+","+n.getWriteDate()+"......");
    }

    @Test
    public void test3(){
        List<News> newsList=newsMapper2.getNewsByKeyword("我");
        for(News n:newsList){
            System.out.println(n.getNewsId()+","+n.getTitle()+","+n.getContext()+","+n.getWriteDate()+"......");
        }
    }

    @Test
    public void test4(){
        int[] ids=new int[]{1,2,3,14};
        List<News> newsList=newsMapper2.getNewsByIds(ids);
        for(News n:newsList){
            System.out.println(n.getNewsId()+","+n.getTitle()+","+n.getContext()+","+n.getWriteDate()+"......");
        }
    }


    @Test
    public void test5(){
        List list=new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        Map map=new HashMap();
        map.put("title","范");
        map.put("ids",list);
        List<News> newsList=newsMapper2.getNewsByIdsAndTitle(map);
        for(News n:newsList){
            System.out.println(n.getNewsId()+","+n.getTitle()+","+n.getContext()+","+n.getWriteDate()+"......");
        }

    }

    @Test
    public void test6(){
        News news=new News();
        news.setTitle("范");
        news.setContext("猿");
        List<News> newsList=newsMapper2.getNewsByTitleAndContext(news);
        for(News n:newsList){
            System.out.println(n.getNewsId()+","+n.getTitle()+","+n.getContext()+","+n.getWriteDate()+"......");
        }
    }

    @Test
    public void test7(){
        List<News> newsList=newsMapper2.testManyToOne();
        for(News n:newsList){
            System.out.println(n.getNewsId()+","+n.getTitle()+","+n.getContext()+","+n.getWriteDate()+"所在板块"+n.getBoard().getBoardName());
        }
    }

    @Test
    public void test8(){
        List<Board> boardList=boardMapper2.testOneToMany();
        for(Board board:boardList){
            System.out.println(board.getBoardName()+"版块下的新闻有");
            List<News> newsList=board.getNewsList();
            for(News n:newsList){
                System.out.println(n.getNewsId()+","+n.getTitle()+",");
            }
        }
    }

    @Test
    public void test9(){
        Board board=boardMapper2.getBoardAndNews(2);
        System.out.println(board.getBoardName()+"板块下面的新闻有：");
        for(News n:board.getNewsList()){
            System.out.println(n.getNewsId()+","+n.getTitle());
        }
    }

    @Test
    public void test10(){
        List<News> newsList=newsMapper2.daYu(10);
        for(News n:newsList){
            System.out.println(n.getNewsId()+","+n.getTitle()+",");
        }


    }
    @After
    public void destory(){
        sqlSession.close();
    }
}
